package com.zwl.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zwl.dao.BaseDao;
import com.zwl.dao.UserDetailDao;
import com.zwl.po.PageBean;
import com.zwl.po.UserDetailPo;

public class UserDetailDaoImpl extends BaseDao<UserDetailPo> implements UserDetailDao {

	@Override
	public List<UserDetailPo> findByCondition(Map<String, Object> map) {
		List<Object> paramList=new ArrayList<>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from b_user_detail where 1=1 ");
		
		Map<String, String> columnsMap=new HashMap<String, String>();
		columnsMap.put("user_id", "id");
		return this.executeQuery3(sf.toString(), columnsMap, UserDetailPo.class, paramList.toArray());
	}

	@Override
	public UserDetailPo findbyId(Integer id) {
		List<Object> paramList=new ArrayList<>();
		String sql=" select * from b_user_detail where user_id=? ";
		paramList.add(id);
		
		Map<String, String> columnsMap=new HashMap<String, String>();
		columnsMap.put("user_id", "id");
		List<UserDetailPo> list=this.executeQuery3(sql, columnsMap, UserDetailPo.class, paramList.toArray());
		return (list!=null && list.size()>0)?list.get(0):null;
	}

	@Override
	public Integer[] saveUserDetail(UserDetailPo entity) {
		String sql="insert into b_user_detail values(?,?,?,?,?,?,?)";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(entity.getUserId());
		paramList.add(entity.getName());
		paramList.add(entity.getSex());
		paramList.add(entity.getTelephone());
		paramList.add(entity.getEmail());
		paramList.add(entity.getRemark());
		paramList.add(entity.getDeptId());
		return (Integer[]) this.executeUpdate(sql,"insert",new String[]{"USER_ID"}, paramList.toArray());
	}

	@Override
	public int updateUserDetail(UserDetailPo entity) {
		StringBuffer sf=new StringBuffer();
		sf.append(" update b_user_detail set ");
		sf.append("    name=? ");
		sf.append("   ,sex=? ");
		sf.append("   ,telephone=? ");
		sf.append("   ,email=? ");
		sf.append("   ,remark=? ");
		sf.append("   ,dept_id=? ");
		sf.append(" where user_id=? ");
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(entity.getName());
		paramList.add(entity.getSex());
		paramList.add(entity.getTelephone());
		paramList.add(entity.getEmail());
		paramList.add(entity.getRemark());
		paramList.add(entity.getDeptId());
		paramList.add(entity.getUserId());
		return (Integer) this.executeUpdate(sf.toString(),"update",null, paramList.toArray());
	}

	@Override
	public UserDetailPo getEntity(ResultSet rs) {
		return null;
	}

	@Override
	public PageBean<UserDetailPo> findPageByCondition(int pageIndex, int pageSize, Map<String, Object> map) {
		PageBean<UserDetailPo> pagebean=new PageBean<UserDetailPo>();
		pagebean.setPageIndex(pageIndex);
		pagebean.setPageSize(pageSize);
		List<Object> paramList=new ArrayList<Object>();
		
		//2、拼接sql
		StringBuffer baseSql=new StringBuffer();
		baseSql.append(" select * from b_user_detail where 1=1 ");
		
		//3、获得总记录
		pagebean.setTotalCount(this.getCount(baseSql.toString(), paramList.toArray()));
		
		//4、获得分页数据
		if(pagebean.getTotalCount()>0){
			StringBuffer pageSql=new StringBuffer();
			pageSql.append(" select * from ( ");
			pageSql.append("  select tmp1.*,rownum rn from ( ");
			pageSql.append(baseSql.toString());
			pageSql.append("  ) tmp1");
			pageSql.append(" ) tmp2");
			pageSql.append(" where tmp2.rn>? and tmp2.rn<=? ");
			int startRow=(pagebean.getPageIndex()-1)*pagebean.getPageSize();
			int endRow=pagebean.getPageIndex()*pagebean.getPageSize();
			paramList.add(startRow);
			paramList.add(endRow);
			
			Map<String, String> columnsMap=new HashMap<String, String>();
			columnsMap.put("user_id", "id");
			pagebean.setList(this.executeQuery3(pageSql.toString(), columnsMap, UserDetailPo.class, paramList.toArray()));
		}
		
		return pagebean;
	}

}
